#include "windows.h"

namespace WINDOW
{
	WinDows::WinDows(std::string Title, SIZE sZ, POINT PT)
	{
		InitWin(Title, sZ, PT);
	}
	WinDows::WinDows(std::string Title, SIZE sZ)
	{
		POINT PT = { CW_USEDEFAULT, CW_USEDEFAULT };
		InitWin(Title, sZ, PT);
	}
	WinDows::WinDows(std::string Title)
	{
		SIZE sZ = { 800, 600 };
		POINT PT = { CW_USEDEFAULT, CW_USEDEFAULT };
		InitWin(Title, sZ, PT);
	}

	void WinDows::InitWin(std::string Windowname, SIZE sz, POINT pt)
	{
		memset(&wndclass, 0, sizeof(wndclass));
		hInstance = GetModuleHandle(nullptr);
		sprintf(mp_Class, "0x%p", this);
			
		wndclass.cbSize = sizeof(wndclass);
		wndclass.style = CS_HREDRAW | CS_VREDRAW ;
		wndclass.lpfnWndProc = &WINDOW::WinDows::WndHandle;
		wndclass.cbClsExtra = 0;
		wndclass.cbWndExtra = 0;
		wndclass.hInstance = hInstance;
		wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
		wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
		wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
		wndclass.lpszClassName = mp_Class;

		if(!RegisterClassEx(&wndclass))
		{
			MessageBoxA(NULL, 
						TEXT("Error: Klasse konnte nicht erstellt werden."),
						NULL,
						MB_OK | MB_ICONERROR);
			ExitProcess(0);
		}

		hwnd = CreateWindow(mp_Class, 
							Windowname.c_str(), 
							WS_OVERLAPPEDWINDOW,
							pt.x,
							pt.y,
							sz.cx,
							sz.cy,
							NULL,
							NULL,
							hInstance,
							NULL);
	}

	LRESULT CALLBACK WinDows::WndHandle(HWND hwnd, UINT uint, WPARAM wparam, LPARAM lparam)
	{
		switch (uint)
		{
		case WM_CREATE:
			break;
		case WM_CLOSE:
			PostQuitMessage(0);
			break;
		}

		return DefWindowProc(hwnd, uint, wparam, lparam);
	};

	BOOL WinDows::RunWin()
	{
		if(!hwnd)
			return false;
		ShowWindow(hwnd, SW_SHOW);
		isrunning = true;
		do
		{
			while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
			{
				TranslateMessage(&msg);
				DispatchMessage(&msg);
			}
			Sleep(10);
		} while (isrunning);

		return true;
	};
}